import { createRouter, createWebHistory } from "vue-router";

const routes = [
  {
    path: "/login",
    name: "Login",
    component: () => import("../views/Login.vue"),
    meta: { title: "登录" },
  },
  {
    path: "/",
    name: "Layout",
    component: () => import("../layout/Layout.vue"),
    meta: {
      requiresAuth: true,
    },
    children: [
      {
        path: "",
        name: "Dashboard",
        component: () => import("../views/Dashboard.vue"),
        meta: { title: "仪表盘", requiresAuth: true },
      },
      {
        path: "command-center",
        name: "CommandCenter",
        component: () => import("../views/CommandCenter.vue"),
        meta: { title: "指挥中心", requiresAuth: true },
      },
      {
        path: "users",
        name: "Users",
        component: () => import("../views/Users.vue"),
        meta: { title: "用户管理", requiresAuth: true },
      },
      {
        path: "roles",
        name: "Roles",
        component: () => import("../views/Roles.vue"),
        meta: { title: "角色管理", requiresAuth: true },
      },
      {
        path: "drones",
        name: "Drones",
        component: () => import("../views/Drones.vue"),
        meta: { title: "无人机管理", requiresAuth: true },
      },
      {
        path: "drone-types",
        name: "DroneTypes",
        component: () => import("../views/DroneTypes.vue"),
        meta: { title: "无人机类型管理", requiresAuth: true },
      },

      {
        path: "departments",
        name: "Departments",
        component: () => import("../views/Departments.vue"),
        meta: { title: "部门管理", requiresAuth: true },
      },
      {
        path: "no-fly-zones",
        name: "NoFlyZones",
        component: () => import("../views/NoFlyZones.vue"),
        meta: { title: "禁飞区管理", requiresAuth: true },
      },
      {
        path: "tasks",
        name: "Tasks",
        component: () => import("../views/Tasks.vue"),
        meta: { title: "任务管理", requiresAuth: true },
      },
      {        path: "routes",        name: "FlightRoutes",        component: () => import("../views/FlightRoutes.vue"),        meta: { title: "航线管理", requiresAuth: true },      },
      {
        path: "dicts",
        name: "Dicts",
        component: () => import("../views/Dict.vue"),
        meta: { title: "数据字典管理", requiresAuth: true },
      },
      {
        path: "menus",
        name: "Menus",
        component: () => import("../views/Menus.vue"),
        meta: { title: "菜单管理", requiresAuth: true },
      },
      {
        path: "detection-models",
        name: "DetectionModels",
        component: () => import("../views/DetectionModels.vue"),
        meta: {
          title: "检测模型管理",
          requiresAuth: true,
        },
      },
      {
        path: "task-scenarios",
        name: "TaskScenarios",
        component: () => import("../views/TaskScenarios.vue"),
        meta: {
          title: "任务场景管理",
          requiresAuth: true,
        },
      },
      {
        path: "operation-logs",
        name: "OperationLogs",
        component: () => import("../views/OperationLogs.vue"),
        meta: { title: "操作日志管理", requiresAuth: true },
      },
      {
        path: "flight-logs",
        name: "FlightLogs",
        component: () => import("../views/FlightLogs.vue"),
        meta: { title: "飞行日志管理", requiresAuth: true },
      },
      {
        path: "detection-logs",
        name: "DetectionLogs",
        component: () => import("../views/DetectionLogs.vue"),
        meta: { title: "检测日志管理", requiresAuth: true },
      },
      {
        path: "exception-logs",
        name: "ExceptionLogs",
        component: () => import("../views/ExceptionLogs.vue"),
        meta: { title: "异常日志管理", requiresAuth: true },
      },
    ],
  },
  {
    path: "/:pathMatch(.*)*",
    name: "NotFound",
    component: () => import("../views/NotFound.vue"),
    meta: { title: "页面未找到" },
  },
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
});

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title
    ? `${to.meta.title} - 无人机管理系统`
    : "无人机管理系统";

  // 检查是否需要登录
  if (to.matched.some((record) => record.meta.requiresAuth)) {
    const token = localStorage.getItem("token");
    if (!token) {
      next({ name: "Login" });
    } else {
      next();
    }
  } else {
    next();
  }
});

export default router;
